package com.hourses.service.impl;

import com.alibaba.fastjson2.JSON;
import com.hourses.mapper.SupplyMapper;
import com.hourses.pojo.Supply;
import com.hourses.service.SupplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Description:
 * @Author:LiTao
 * @Date:2025/5/6 16:01
 */
@Service
public class SupplyServiceImpl implements SupplyService {
    //查询所有供应商
    @Autowired
    private SupplyMapper supplyMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public List<Supply> selectSupplyList() {
        //先看看缓存里有没有
        String allSupplies = redisTemplate.opsForValue().get("allSupplies");
        if (StringUtils.hasText(allSupplies)){
            //有缓存的情况
            return JSON.parseArray(allSupplies,Supply.class);
        }
        //没有缓存的情况
        List<Supply> supplies = supplyMapper.selectSupplyList();
        //将查询结果存入缓存当中
        redisTemplate.opsForValue().set("allSupplies",JSON.toJSONString(supplies));
        return supplies;
    }
}
